<?php echo $this->element('displayHeader'); ?>

<?php if ($session->read('Auth.User.prefix') == User::USER_PREFIX_CUSTOMER) { ?>
    <h2><div class="inline-block" style="width: 80px;">Vendor:</div><?php echo $supplier['Supplier']['name']; ?></h2>
<?php } else { ?>
    <h2><div class="inline-block" style="width: 80px;">Customer:</div><?php echo $customer['Customer']['name']; ?></h2>
    <h2><div class="inline-block" style="width: 80px;">Site:</div><?php echo $site['Site']['name']; ?></h2>
<?php } ?>
<br />
<br />

<div class="toolbar" style="clear: both; position: relative; top: -5px"><?php
    echo $this->element('toolbar', array('buttons' => array(
            array('search' => array('params' => 'supplier_id:' . $supplier_id . '/customer_id:' . $customer_id . '/site_id:' . $site_id)),
            array('refresh' => array('controller' => 'supplier_inventories', 'action' => 'refresh', 'params' => $customer_id . '/' . $site_id . '/' . '/supplier_id:' . $supplier_id)),
            )));
    ?></div>

<?php if (empty($this->data)) : // ถ้าไม่มีข้อมูล          ?>
    <?php echo $this->element('dataNotFound'); ?>
<? else : // ถ้ามีข้อมูล        ?>
    <?php $paginator->options(array('url' => array('controller' => 'supplier_inventories', 'action' => 'display', $customer_id, $site_id, 'supplier_id' => $supplier_id))); ?>
    <?php echo $this->element('dataCaptionLimit', array('controller' => 'supplier_inventories', 'action' => 'display', 'params' => $customer_id . '/' . $site_id . '/' .  '/supplier_id:' . $supplier_id)); ?>
    <?php $cur = $paginator->counter(array('format' => '%start%')); ?>

    <table class="data">
        <thead>
            <tr>
                <th class="no"><?php echo $paginator->sort('No', 'id'); ?></th>
                <th>Vendor Part no.</th>
                <th><?php echo $paginator->sort('Stock Code', 'CustProductSite.stock_code'); ?></th>
                <th><?php echo $paginator->sort('Category', 'Product.Category.name'); ?></th>
                <th><?php echo $paginator->sort('Brand', 'Product.Brand.name'); ?></th>
                <th><?php echo $paginator->sort('Model', 'Product.model'); ?></th>
                <th><?php echo $paginator->sort('Description', 'Product.title'); ?></th>
    <!--                <th>Location</th>-->
                <th><?php echo $paginator->sort('C/S', 'CustProductSite.supplier_supplier_current_stock'); ?></th>
                <th><?php echo $paginator->sort('Request', 'CustProductSite.request_qty'); ?></th>
                <th><?php echo $paginator->sort('Min', 'CustProductSite.min_stock'); ?></th>
                <th><?php echo $paginator->sort('Max', 'CustProductSite.max_stock'); ?></th>
                <th width="50px">&nbsp;</th>
            </tr>
        </thead>
        <tbody>
            <?php
            foreach ($this->data as $i => $data):
                $supplier_current_stock = $data['CustProductSite']['supplier_current_stock'];
                $min_stock = $data['CustProductSite']['min_stock'];
                $max_stock = $data['CustProductSite']['max_stock'];
                ?>
                <tr class="<?php
        if (empty($max_stock) && empty($min_stock) || $supplier_current_stock == $max_stock) {
            echo (($i + 1) % 2 == 0) ? 'even-cell' : 'odd-cell';
        } else {
            echo $supplier_current_stock <= $min_stock ? 'min-stock' : ($supplier_current_stock < $max_stock ? 'max-stock' : ($supplier_current_stock > $max_stock ? 'green' : ''));
        }
                ?>">
                    <td class="center"><?php echo $cur; ?></td>
                    <td><?php echo $data['Product']['supplier_part_no']; ?></td>
                    <td><?php echo $data['CustProductSite']['stock_code']; ?></td>
                    <td><?php echo isset($data['Product']['Category']['name']) ? $data['Product']['Category']['name'] : '-'; ?></td>
                    <td><?php echo $data['Product']['Brand']['name']; ?></td>
                    <td><?php echo $data['Product']['model']; ?></td>
                    <td><?php echo!empty($data['CustProductSite']['text']) ? $data['CustProductSite']['text'] : $data['Product']['text']; ?></td>
        <!--                    <td><?php //echo isset($data['ProductSerial'][0]) && isset($data['ProductSerial'][0]['CustLocation']['name']) ? $data['ProductSerial'][0]['CustLocation']['name'] : '-'; ?></td>-->
                    <td class="text-right">
                        <?php echo $supplier_current_stock; ?>
                    </td>
                    <td class="text-right">&nbsp;<?php echo $data['CustProductSite']['request_qty']; ?></td>
                    <td class="text-right">&nbsp;<?php echo $data['CustProductSite']['min_stock']; ?></td>
                    <td class="text-right">&nbsp;<?php echo $data['CustProductSite']['max_stock']; ?></td>
                    <td>
                        <?php echo $html->link($html->image('icon/edit.png', array('title' => 'view', 'alt' => 'view')), '/supplier_inventories/view/' . $data['CustProductSite']['id'], null, null, false); ?>&nbsp;
                    </td>
                </tr>
            <?php ++$cur; endforeach; ?>
        </tbody>
    </table>
    <?php echo $this->element('paging'); ?>
    <div class="caption">
        <span class="min-stock rect"></span> Current Stock is less than Min Stock.
        <span class="max-stock rect"></span> Current Stock is less than Max Stock.
        <span class="green rect"></span> Current Stock is higher than Max Stock.
    </div>

<? endif; ?>

<script type="text/javascript">
    $j(function() {

        $j("#viewSiteInventory").bind("change", function(event) {
            window.location = base + '/supplier_inventories/display/<?php echo $customer_id; ?>/' + $j(this).val();
        });
    });
</script>